package com.jerry.controller;

import com.jerry.DO.FileChunkMeta;
import com.jerry.DO.MetaFile;
import com.jerry.DTO.CompleteChunkFileDTO;
import com.jerry.DTO.FileMeta;
import com.jerry.response.CommonResponse;
import com.jerry.service.MetaService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RequestMapping("/meta")
@RestController
public class MetaController {
    private final MetaService metaService;

    public MetaController(MetaService metaService) {
        this.metaService = metaService;
    }

    @PostMapping("/generate")
    public CommonResponse<MetaFile> generate(@RequestBody FileMeta fileMeta) {
        MetaFile metafile = metaService.generate(fileMeta);
        return CommonResponse.success(metafile);
    }

    @PostMapping("/chunk/complete")
    public CommonResponse chunkComplete(@RequestBody CompleteChunkFileDTO completeChunkFileDTO) {
        metaService.complteteChunk(completeChunkFileDTO);
        return CommonResponse.success();
    }

    @GetMapping("/info")
    public CommonResponse<MetaFile> info(@RequestParam("bucketName") String bucketName, @RequestParam("filename") String filename) {
        MetaFile metaFile = metaService.meta(bucketName, filename);
        return CommonResponse.success(metaFile);
    }

    @GetMapping("/files/{fileType}")
    public CommonResponse<List<MetaFile>> files(@PathVariable String fileType){
        List<MetaFile> metaFiles = metaService.files(fileType);
        return CommonResponse.success(metaFiles);
    }

    @GetMapping("/chunk/info")
    public CommonResponse<List<FileChunkMeta>> chunkInfo(@RequestParam("bucketName") String bucketName,
                                                         @RequestParam("fileName") String fileName,
                                                         @RequestParam("chunkNo") Integer chunkNo){
        List<FileChunkMeta> fileChunkMetas = metaService.chunkInfo(bucketName,fileName,chunkNo);
        return CommonResponse.success(fileChunkMetas);
    }

    @DeleteMapping("/{bucketName}/{fileName}")
    public CommonResponse delete(@PathVariable String bucketName ,
                                 @PathVariable String fileName){
        metaService.delete(bucketName,fileName);
        return CommonResponse.success();
    }
}
